package org.szy.plugin.sys.controller;


import org.noear.solon.annotation.*;
import org.noear.solon.core.handle.ModelAndView;
import org.noear.solon.validation.annotation.NotBlank;
import org.noear.solon.validation.annotation.NotNull;
import org.noear.solon.validation.annotation.Validated;
import org.szy.core.common.controller.BaseController;
import org.szy.core.common.entity.R;
import org.szy.plugin.sys.entity.SysUser;
import org.szy.plugin.sys.param.SysUserRoleAddParam;
import org.szy.plugin.sys.service.SysUserRoleService;

/**
 * 用户和角色关联表控制器
 *
 * @author wangbuhao
 * @since 2023-06-27 09:56:03
 */
@Controller
@Mapping("/sys/userRole")
public class SysUserRoleController extends BaseController<SysUser> {

    private final String PATH_PREFIX = "sys/userRole/";
    @Inject
    private SysUserRoleService sysUserRoleService;


    /**
     * 授予角色
     */
    @Get
    @Mapping("/grantRole")
    public ModelAndView grantRole() {
        return new ModelAndView(PATH_PREFIX + "grantRole");
    }

    /**
     * 获取拥有的角色
     *
     * @param userId 用户标识
     */
    @Get
    @Mapping("/ownRole")
    public R<?> ownRole(@NotNull Long userId) {
        return R.data(sysUserRoleService.listByUserId(userId));
    }

    /**
     * 保存角色和角色关联
     *
     * @param param 添加参数
     */
    @Post
    @Mapping("/saveUserRole")
    public R<?> saveUserRole(@Validated SysUserRoleAddParam param) {
        boolean save = sysUserRoleService.add(param);
        return R.bool(save);
    }

}
